//
//  YTFRepostView.swift
//  sinaWeibo
//
//  Created by pro on 16/3/2.
//  Copyright © 2016年 pro. All rights reserved.
//

import UIKit
import SnapKit

class YTFRepostView: UIView {
    
    //定义一个约束来记录
    var bottomConstraint: Constraint?
    
    //定义一个属性
    var repostViewModel: YTFStatusViewModel?{
        didSet{
            contentLabel.text = repostViewModel?.statusModel?.retweeted_status?.text
            
            //清空原有约束
            bottomConstraint?.uninstall()
            
            if let pic_url = repostViewModel?.statusModel?.retweeted_status?.pic_urls where pic_url.count > 0 {//有配图
                
                pictureView.hidden = false
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                
                //传值
                //定义一个临时数组进行数据拼接
                var result = [YTFPictureViewModel]()
                
                for model in pic_url {
                    let viewModel = YTFPictureViewModel()
                    viewModel.model = model
                    result.append(viewModel)
                }
                pictureView.pictureList = result
                
            }else{
                
                pictureView.hidden = true
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
                
            }
            
        }
    }
    
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加视图并设置约束
    private func setupUI(){
        
        backgroundColor = UIColor(white: 195/225.0, alpha: 1)
        
        //添加子控件
        addSubview(contentLabel)
        addSubview(pictureView)
        pictureView.backgroundColor = UIColor(white: 195/225.0, alpha: 1)
        //添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left).offset(CellMargin)
//            make.bottom.equalTo(self.snp_bottom)
            make.right.equalTo(self.snp_right).offset(-CellMargin)
        }
        
        //默认为有配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(contentLabel.snp_bottom)
            
            //给一个固定宽高
            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        
        //转发微博的视图底部约束
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
        
    }
    
    //懒加载
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "转发微博"
        
//        label.backgroundColor = UIColor(white: 210/225.0, alpha: 1)
        
        label.textColor = UIColor(white: 100/255.0, alpha: 1)
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.numberOfLines = 0
        
        return label
    }()
    
    
    //配图
    lazy var pictureView: YTFPictureView = YTFPictureView()

}
